<?php

namespace frontend\controllers;

use yii\web\Controller;
use frontend\models\UserdetailForm;
use frontend\models\City;
use yii\helpers\ArrayHelper;
use frontend\models\UserForm;
use yii\helpers\Html;
use frontend\models\TbaMembeaddrinfo;
use yii\base\Exception;
use frontend\models\TbaMemberinforecordinfo;
use frontend\models\TbaMemberinfo;
class UserdetailController extends Controller {
	
	//会员详细信息，默认的操作
	public function actionIndex(){
		
		//当前用户的用户名
		$username=\Yii::$app->session->get('username');
		//当前用户账户余额
		$balance=TbaMemberinfo::findOne(['username'=>$username])->Balance;
		return $this->render('index',['balance'=>$balance]);
	}
	
	//填写收货地址操作
	public function actionAddress(){
		$addressModel=new UserdetailForm();
		$addressModel->scenario='address';

		//查询省份
		$province=City::find()->where('parent_id=0')->all();
		$provincedata=ArrayHelper::map($province, 'region_id', 'region_name');
		//判断是否有表单提交动作
		if(\Yii::$app->request->isPost){
			//有提交动作进行赋值
			$addressModel->attributes=\Yii::$app->request->post('UserdetailForm');			
			//进行表单验证
			if($addressModel->validate()){				
				//进行数据写入
				$memadd=new TbaMembeaddrinfo();
				//用户名流水号id赋值
				$memadd->TBa_ID=\Yii::$app->session->get('userid');
				//用户名赋值
				$memadd->username=\Yii::$app->session->get('username');
				//收货人姓名赋值
				$memadd->RecMan=$addressModel->RecMan;
				//收货人电话赋值
				$memadd->Tel=$addressModel->Tel;
				//查询省份名称
				$province_id=$addressModel->province;
				$provincename=City::findOne(['region_id'=>$province_id])->region_name;
				//查询城市名称
				$city_id=$addressModel->city;
				$cityname=City::findOne(['region_id'=>$city_id])->region_name;	
				//查询区域名称
				$areaname=($addressModel->area=='prompt')?"":((City::findOne(['region_id'=>$addressModel->area])->region_name));
				//详细地址赋值
				$detailaddress=$addressModel->address;
				$memadd->RecAddress=$provincename.$cityname.$areaname.$detailaddress;
				//邮编赋值
				$memadd->PostCode=$addressModel->PostCode;
				//是否默认赋值
				$memadd->IsDefault=$addressModel->IsDefault;
				//数据存储到数据库
				if($memadd->save()){
					$this->redirect(['userdetail/index']);
				}else{
					throw new Exception('数据写入失败', '404');
				}
			}
		}
		return $this->render('address',[
				'model'=>$addressModel,
				'province'=>$provincedata,
			
		]);
	}
	public function actionCity(){
		if(\Yii::$app->request->post('pid') && \Yii::$app->request->post('city') && \Yii::$app->request->post('city')=='1'){
			$pid=\Yii::$app->request->post('pid');
			$city=City::find()->where(['parent_id'=>$pid])->asArray()->all();
			$data="";
			foreach($city as $v){
				$data.="<option value=\"".$v['region_id']."\">".$v['region_name']."</option>";
			}
			return $data;
		}
		
	}
	public function actionArea(){
		if(\Yii::$app->request->post('pid') && \Yii::$app->request->post('area') && \Yii::$app->request->post('area')=='1'){
			$pid=\Yii::$app->request->post('pid');
			$area=City::find()->where(['parent_id'=>$pid])->asArray()->all();
			$data="";
			foreach ($area as $a){
				$data.="<option value=\"".$a['region_id']."\">".$a['region_name']."</option>";
			}
			return $data;
		}
	}
	//充值操作
	public function actionRecharge(){
		$rechargeModel=new UserdetailForm(['scenario'=>'recharge']);
		//当前用户的用户名
		$username=\Yii::$app->session->get('username');
		//当前用户账户余额
		
		if(\Yii::$app->request->isPost){
			$rechargeModel->attributes=\Yii::$app->request->post('UserdetailForm');
			//表单验证
			if($rechargeModel->validate()){
				$charge=new TbaMemberinforecordinfo();
				$charge->ID=\Yii::$app->session->get('userid');
				$charge->username=\Yii::$app->session->get('username');
				$charge->attributes=$rechargeModel->attributes;
				$charge->SupplyTime=date("Y-m-d h:i:s",time());
				 if($charge->save()){
				 	//更新用户余额
				 	$membalance=TbaMemberinfo::findOne(['username'=>$username]);
				 	$membalance->Balance =($membalance->Balance)+($charge->TotalMoney);
				 	if($membalance->save()){
				 		
				 		$this->redirect(['userdetail/index']);
				 		
				 	}else{
				 		//var_dump($membalance->errors);
				 		throw new Exception('数据写入失败', '404');
				 	}
				 	
				}else{
					throw new Exception('数据写入失败', '404');
				} 
			}			
		}
		return $this->render('recharge',['model'=>$rechargeModel]);
	}
	
	//提现操作
	public function actionWithdrawcash(){
		$withdrawcashModel=new UserdetailForm(['scenario'=>'withdrawcash']);
		$username=\Yii::$app->session->get('username');
		$userid=\Yii::$app->session->get('userid');
		if(\Yii::$app->request->isPost){
			$withdrawcashModel->attributes=\Yii::$app->request->post('UserdetailForm');
			if($withdrawcashModel->validate()){
				$withdrawcash=new TbaMemberinforecordinfo();
				$withdrawcash->ID=$userid;
				$withdrawcash->username=$username;
				$withdrawcash->attributes=$withdrawcashModel->attributes;
				$withdrawcash->SupplyTime=date("Y-m-d h:i:s",time());
				//验证成功，首先要根据用户进行判断，提现金额大于账号余额时提示余额不足
				$membalance=TbaMemberinfo::findOne(['username'=>$username]);
				$balance=$membalance->Balance;
				if($withdrawcash->TotalMoney>$membalance){
					throw new Exception('账户余额不足', '404');
				}else{
					//数据写入，跳转页面
					if($withdrawcash->save()){
						//更新用户的余额
						$membalance->Balance -= $withdrawcashModel->TotalMoney;
						if($membalance->save()){
							$this->redirect(['userdetail/index']);
						}else{
							throw new Exception('数据写入失败', '404');
						}
						
					}else{
						throw new Exception('数据写入失败', '404');
					}
				}
				
			
			}
		}
		return $this->render('withdrawcash',['model'=>$withdrawcashModel]);
	}
}

?>